//
//  RWTabBar.m
//  baisibudejie
//
//  Created by renwei on 16/9/18.
//  Copyright © 2016年 renwei. All rights reserved.
//

#import "RWTabBar.h"

@interface RWTabBar()

@property (nonatomic, weak) UIButton *plusButton;
//**上次点击的按钮*/
@property (nonatomic, strong) UIControl *previousClickedTabBarButton;

@end

@implementation RWTabBar

- (UIButton *)plusButton {
    
    if (_plusButton == nil) {
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted];
        [btn sizeToFit];
        [self addSubview:btn];
        
        _plusButton = btn;
    }
    return _plusButton;
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    // 跳转tabBarButton位置
    NSInteger count = self.items.count;
    
    //    self.width;
    
    CGFloat btnW = self.rw_width / (count + 1);
    CGFloat btnH = self.rw_height;
    CGFloat x = 0;
    int i = 0;
    // 私有类:打印出来有个类,但是敲出来没有,说明这个类是系统私有类
    // 遍历子控件 调整布局
    for (UIControl *tabBarButton in self.subviews) {
        
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            
            // 第一个为选中的按钮
            if (i == 0 && self.previousClickedTabBarButton == nil) {
                self.previousClickedTabBarButton = tabBarButton;
            }
  
            if (i == 2) {
                i += 1;
            }
            
            x = i * btnW;
            
            tabBarButton.frame = CGRectMake(x, 0, btnW, btnH);
            
            i++;
            
            // 监听点击事件
            [tabBarButton addTarget:self action:@selector(tabBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
      
        }
    }
    
    // 调整发布按钮位置
    self.plusButton.center = CGPointMake(self.rw_width * 0.5, self.rw_height * 0.5);
}

#pragma mark - 监听方法
- (void)tabBarButtonClick:(UIControl *)tabBarButton {

    if (self.previousClickedTabBarButton == tabBarButton) {
//        RWFunc;
        // 通过通知的方式传播出去（告知外界按钮被电击）
        // 通知名 前缀 ＋ 主体 ＋ did/will + 动词 ＋ 通知名
        [[NSNotificationCenter defaultCenter] postNotificationName:RWTabBarButtonDidTepeatClickNotification object:nil];
    }
    self.previousClickedTabBarButton = tabBarButton;
}

@end
